<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Array functions</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="PHP, learn PHP, array, functions programming, language">
<meta name="description" content="In this part of the PHP tutorial, we cover PHP Array functions.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Array functions</h1>

<p>
In the previous chapter, we dealt with array initialization and 
perusal. In this chapter, we will
cover various PHP array functions. These functions enable us to 
modify, sort, merge, slice, shuffle the data inside
the arrays.
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* top_horizontal */
google_ad_slot = "3327173442";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
First we are going to sort an array.
</p>

<pre class="code">
&lt;?php

$names = array("Jane", "Rebecca", "Lucy", "Lenka", "Ada");

foreach ($names as $name) {
    echo "$name ";
}

echo "\n";

sort($names);

foreach ($names as $name) {
    echo "$name ";
}

echo "\n";

?&gt;
</pre>

<p>
In the above script, we have a <b>$names</b> array. We use the 
<code>sort()</code> function to sort the contents of the array. 
</p>

<pre>
$ php sort.php 
Jane Rebecca Lucy Lenka Ada 
Ada Jane Lenka Lucy Rebecca 
</pre>

<p>
The output of the script shows unsorted and sorted female names.
</p>

<pre class="code">
&lt;?php

$numbers = array(1, 2, 4, 5, 2, 3, 5, 2);

$nums = count($numbers);
$sum = array_sum($numbers);

echo "In the array, there are $nums numbers\n";
echo "The sum of the numbers is $sum\n";

?&gt;
</pre>

<p>
In the example, we have an array of numbers. We use the <code>count()</code> function
to count the number of elements in the array. And the <code>array_sum()</code> function
to calculate the sum of all values. 
</p>

<pre>
$ php sumcount.php 
In the array, there are 8 numbers
The sum of the numbers is 24
</pre>

<p>
This is the output of the script. 
</p>

<pre class="code">
&lt;?php

$numbers = array(3, 4, 4, 3, 2, 4);
$count_values = array_count_values($numbers);

print_r($count_values);

$unique = array_unique($numbers);

print_r($unique);

?&gt;
</pre>

<p>
In this script, we have duplicates in the array. The <code>array_count_values()</code>
function returns an array with the number of occurrences for each value. 
The <code>array_unique()</code> function returns an array without duplicates.
</p>

<pre>
$ php unique.php 
Array
(
    [3] => 2
    [4] => 3
    [2] => 1
)
Array
(
    [0] => 3
    [1] => 4
    [4] => 2
)
</pre>

<p>
The first array says, that 3 is
present twice, 4 three times and 2 once.
The second array says that there are three values present in the array. 3, 4 and 2.
3 has index 0, 4 has index 1 and 2 has index 4. The <code>array_unique()</code>
function keeps the indexes untouched. 
</p>

<pre class="code">
&lt;?php

$continents = array("America", "Africa", "Europe", "Asia", "Antarctica");

$item1 = current($continents);
$item2 = next($continents);
$item3 = next($continents);
$item4 = end($continents);
$item5 = prev($continents);

echo "$item1, $item2, $item3, $item4, $item5\n";

reset($continents);

while(list($idx, $val) = each($continents)) {
    echo "Index: $idx, Value: $val\n";
}

?&gt;
</pre>

<p>
In this example, we traverse the array using the functions, that move the internal array
pointer. 
</p>

<pre class="explanation">
$item1 = current($continents);
$item2 = next($continents);
$item3 = next($continents);
$item4 = end($continents);
$item5 = prev($continents);
</pre>

<p>
The <code>current()</code> function returns the current element in the array. At the beginning, it 
is the first element of the array. The <code>next()</code> function
advances the pointer by one position. The <code>end()</code> function returns the last element. 
The <code>prev()</code> element returns the element, one position before the current one. In our case
it is the next to the last element. 
</p>

<pre class="explanation">
reset($continents);

while(list($idx, $val) = each($continents)) {
    echo "Index: $idx, Value: $val\n";
}
</pre>

<p>
Here we use the <code>reset()</code> function to set the internal pointer to the
first element again. And peruse the <b>$continents</b> array one more time. 
</p>

<pre class="code">
&lt;?php

$names1 = array("Jane", "Lucy", "Rebecca");
$names2 = array("Lenka", "Timea", "Victoria");

$names = array_merge($names1, $names2);

foreach ($names as $name) {
    echo "$name ";
}

echo "\n";

?&gt;
</pre>

<p>
In this example, we have two arrays. <b>$names1</b> and <b>$names2</b>. We 
use the <code>array_merge()</code> function to create <b>$names</b> array by merging the previous two arrays.  
</p>

<pre>
$ php merge.php 
Jane Lucy Rebecca Lenka Timea Victoria 
</pre>

<p>
The new array has six names. 
</p>

<pre class="code">
&lt;?php

$numbers = array(1, 2, 3, 4);

array_push($numbers, 5, 6);

foreach ($numbers as $num) {
    echo $num, " ";   
}

echo "\n";

array_pop($numbers);

foreach ($numbers as $num) {
    echo $num, " ";   
}

echo "\n";

array_unshift($numbers, -1, 0);

foreach ($numbers as $num) {
    echo $num, " ";   
}

echo "\n";

array_shift($numbers);

foreach ($numbers as $num) {
    echo $num, " ";   
}

echo "\n";

?&gt;
</pre>

<p>
In the above script, we use functions that modify the contents of an array. We have a <b>$nubmers</b> array,
that has 4 numbers. 1, 2, 3, 4.
</p>

<pre class="explanation">
array_push($numbers, 5, 6);
</pre>

<p>
The <code>array_push()</code> function inserts one or more items to the end
of the array. Our array now has 1, 2, 3, 4, 5, 6.
</p>

<pre class="explanation">
array_pop($numbers);
</pre>

<p>
The <code>array_pop()</code> function removes the last item from the array.
Our array stores now 1, 2, 3, 4, 5. 
</p>

<pre class="explanation">
array_unshift($numbers, -1, 0);
</pre>

<p>
The <code>array_unshift()</code> function adds -1, 0 to the beginning of the array.
The array has -1, 0, 1, 2, 3, 4, 5.
</p>

<pre class="explanation">
array_shift($numbers); 
</pre>

<p>
Finally, the <code>array_shift()</code> function removes the first item
from the array. Now we have 0, 1, 2, 3, 4, 5 in the array.
</p>


<pre class="code">
&lt;?php

$numbers = range(1, 15);

foreach ($numbers as $number) {
    echo "$number ";
}

echo "\n";

shuffle($numbers);

foreach ($numbers as $number) {
    echo "$number ";
}

echo "\n";

?&gt;
</pre>

<p>
In this script, we have two array functions. The <code>range()</code> function
enables us to create a list of consecutive numbers easily. In our case, numbers 1, 2, ..15. 
We do not have to type them manually. And the <code>shuffle()</code> function
shuffles e.g. randomly relocates the elements of the array. 
</p>

<pre>
$ php range.php 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
9 15 13 7 3 10 11 4 12 5 6 14 1 2 8 
</pre>

<p>
Your output might look like this. 
</p>

<pre class="code">
&lt;?php

$names = array("Jane", "Adriana", "Lucy", "Rebecca");

if (in_array("Jane", $names)) {
    echo "Jane is in the array\n";
} else {
    echo "Jane is not in the array\n";
}

?&gt;
</pre>

<p>
The <code>in_array()</code> function checks, if a specific element
is inside an array. Our script checks, if 'Jane' is in the <b>$names</b> array.
The name is there, so we get the 'Jane is in the array' message to the console. 
</p>

<pre class="code">
&lt;?php

$domains = array("sk" => "Slovakia", "de" => "Germany",
    "hu" => "Hungary", "ru" => "Russia");

$keys = array_keys($domains);
$values = array_values($domains);

foreach ($keys as $key) {
    echo "$key ";
}

echo "\n";

foreach ($values as $value) {
    echo "$value ";
}

echo "\n";

?&gt;
</pre>

<p>
The <code>array_keys()</code> function returns all the keys of an array. 
The <code>array_values()</code> function returns all the values of an array.
</p>

<pre>
$ php keysvalues.php 
sk de hu ru 
Slovakia Germany Hungary Russia 
</pre>

<p>
The first line consists of top level domain names. These were the keys of the
<b>$domains</b> array. The second line are the names of the corresponding countries. 
These were the values of the array. 
</p>

<pre class="code">
&lt;?php

$countries = array("de" => "Germany", "sk" => "Slovakia",
    "us" => "United States", "ru" => "Russia",
    "hu" => "Hungaria", "pl" => "Poland");

function show_values($value, $key)
{
    echo "The $key stands for the $value\n";
}

array_walk($countries, show_values);

?&gt;
</pre>

<p>
Finally, in our last example, we mention the <code>array_walk()</code> 
function. This function applies a user defined function to every member of the array. We have a 
<b>$countries</b> array. We apply the <b>show_values</b> function to 
each element of the array. The function simply prints the key and the value
for each element. 
</p>


<p>
In this part of the PHP tutorial, we covered some PHP array functions. 
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br> 


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified February 1, 2010  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

